<template>
  <div>
    <el-tree
      ref="refTree"
      :data="list"
      :props="{label:'name'}"
      show-checkbox
      node-key="id"
      default-expand-all
      :check-strictly="true"
    />
    <el-button>取消</el-button>
    <el-button type="primary" @click="hSubmit">确定</el-button>
  </div>
</template>

<script>
import { assignPerm, getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
import { getRoleDeatil } from '@/api/settings'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loaPermissionList()
  },
  methods: {
    async  hSubmit() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      const res = await assignPerm({
        id: this.curId,
        permIds
      })
      console.log(res)
      // 提示
      this.$message.success('保存角色的权限成功')
      this.$emit('close')
    },
    async loaPermissionList() {
      const res = await getPermissionList()
      const detail = await getRoleDeatil(this.curId)
      console.log(detail)
      this.$refs.refTree.setCheckedKeys(detail.data.permIds)
      this.list = tranListToTreeData(res.data)
    }

  }
}
</script>
